/**
 * http://blog.shiqichan.com/introducing-mqtt/
 */

var mqtt = require('mqtt'); 
var settings = {
  host: 'localhost', 
  port: 1883 ,
  keepalive: 10,
  protocolId: 'MQIsdp',
  protocolVersion: 3,
  clientId: 'client-a'
};
client = mqtt.connect(settings);
/**
 * Instant Gratification  即时消息  精确模式
 */
client.subscribe("mqtt/test-1");

/**
 * Pattern Matching   模式匹配   模糊主题
 */
client.subscribe("mqtt/+");


client.publish("mqtt/test-1", "test-1 hello world!");
client.publish("mqtt/test-2", "test-2 hello world!");
client.publish('testMessage', settings.clientId+': 发布new测试信息0',{qos:1,retain: true});
client.publish('testMessage', settings.clientId+': 发布new测试信息1',{qos:1,retain: true});
client.publish('testMessage', settings.clientId+': 发布new测试信息2',{qos:1,retain: true});
client.publish('testMessage', settings.clientId+': 发布new测试信息3',{qos:1,retain: true});

client.on("message", function(topic, payload) {
  console.log([topic, settings.clientId+'--->'+payload].join(": "));
  setTimeout(client.end(),100);
});






